﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LiveCPEWinForm.Gestionnaire;

namespace LiveCPEWinForm
{
    public partial class FmLogin : Form
    {
        GestAPP gestAPP;
        bool logging;
        public FmLogin(GestAPP pGestAPP)
        {
            InitializeComponent();
            // Gestionnaire Application
            gestAPP = pGestAPP;
            // Evenement
            gestAPP.Changed += new GestAPP.StatusChange(gestAPP_Changed);
            // Init de la fenetre
            UpdateScreen(gestAPP.ConnectionStatus, gestAPP.ConnectState);
        }

        void gestAPP_Changed(object sender, GestAPP.ChangeEventArgs e)
        {
            // MAJ sur EVT
            UpdateScreen(e.StatusString, e.StatusCode);
        }

        private void UpdateScreen(String pStatusString, int pStatusCode)
        {
            lblStatus.Text = pStatusString;
            btnLogin.Enabled = true;
            switch (pStatusCode)
            {
                case 0: btnLogin.Text = "Login";
                    break;
                case 1: btnLogin.Text = "Disconnect";
                    if (logging)
                    {
                        logging = false;
                        this.Dispose();
                    }
                    break;
                case 2: btnLogin.Enabled = false;
                    break;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (gestAPP.ConnectState == 0)
            {
                gestAPP.doLogin(tbUserName.Text, tbPassword.Text);
                logging = true;
            }
            else
            {
                gestAPP.doDisconnect();
                tbUserName.Text = gestAPP.UserName;
                tbPassword.Text = gestAPP.Password;             
            }

        }

        private void FmLogin_Load(object sender, EventArgs e)
        {
            if (gestAPP.ConnectState == 1)
            {
                
                tbUserName.Text = gestAPP.UserName;
                tbPassword.Text = gestAPP.Password;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {


        }
        private void FmLogin_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(this.btnLogin, null);
            }
        }
    }
}
